package cn.fl.customer.controller;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.cmm.cache.framework.utils.PropertiesCacheUtils;
import cn.fl.customer.service.ICstCertInfoService;
import cn.fl.customer.vo.CstCertRecVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.system.constant.SysLogConstant;
import com.alibaba.fastjson.JSONObject;
import fl.customer.facade.customer.condition.CstCertRecQC;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * @Description: 客户证件表 控制层
 */
@Controller
@RequestMapping("/customer/cstCertinfo")
public class CstCertinfoController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(CstCertinfoController.class);

	/**
	 * 客户证件信息服务
	 */
	@Autowired
	private ICstCertInfoService cstCertInfoService;

	/**
	 * 数据字典服务
	 */
	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService;

	/**
	 * 到证件配置信息主页面
	 */
	@RequestMapping(value = "/toListPage.do", method = { RequestMethod.GET })
	public ModelAndView toMainPage(HttpServletRequest request, HttpServletResponse response) {
		BaseLogUtils.info(logger, "toList", "跳转到证件管理信息页面.", CurrentThreadContext.getCurrentUserName());
		ModelAndView view = new ModelAndView("customer/cstCertinfo/cstCertInfoList");
		//证件类型
		String codeGrpCd = PropertiesCacheUtils.getProperty(fl.constant.facade.framework.constant.DictionaryConstant.CODE_GRP_CERT_TYP_CD);
		List<KeyValueVO> list = this.sysDictionaryApiService.getKvList(codeGrpCd, CurrentThreadContext.getFlServiceSysCd(), CurrentThreadContext.getCurrentOrgCd());
		view.addObject("certTypeList", list);
		return view;
	}

	/**
	 * 初始化列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initList.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object initCstCertList(HttpServletRequest request, HttpServletResponse response) {
		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		String certTypeCd = request.getParameter("certTypeCd");//证件类型
		String certNm = request.getParameter("certNm");//证件姓名
		String certNo = request.getParameter("certNo");//证件号
		Pager page = new Pager();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}
		CstCertRecQC qc = new CstCertRecQC();
		if (StringUtils.isNotEmpty(certTypeCd)) {
			qc.setCertTypCd(certTypeCd);
		}
		if (StringUtils.isNotEmpty(certNm)) {
			qc.setCertNm(certNm);
		}
		if (StringUtils.isNotEmpty(certNo)) {
			qc.setCertNo(certNo);
		}
		page.setCondition(qc);

		Pager pager = this.cstCertInfoService.searchListPage(page);
		List<CstCertRecVO> list = (List<CstCertRecVO>) pager.getResultList();
		ListPageVO<CstCertRecVO> listPage = new ListPageVO<CstCertRecVO>(list, pager.getTotalCount());
		return listPage;
	}

	/**
	 * 新增
	 */
	@RequestMapping(value = "/add.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object addCstCert(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_ADD_SUCC);
		CstCertRecVO entity = new CstCertRecVO();
		try {
			entity = this.getRequestParamForRskAdd(request);//获取参数
			Long id = this.cstCertInfoService.add(entity);
			result.setData(id);
			BaseLogUtils.info(logger, "addCstCert", "新增客户证件记录表信息成功,ID:"+id+".DATA:"+ JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "addCstCert", "新增客户证件记录表信息失败.DATA:"+JSONObject.toJSONString(entity)+".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "addCstCert", "新增客户证件记录表信息失败,系统错误.DATA:"+JSONObject.toJSONString(entity)+".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}

		return result;
	}

	/**
	 * 修改
	 */
	@RequestMapping(value = "/modify.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object modifyCstCert(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES,SysLogConstant.LOG_CMM_UPDATE_SUCC);
		CstCertRecVO entity = new CstCertRecVO();
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			BaseLogUtils.info(logger, "modifyCstCert", "修改客户证件记录表信息,主键参数为空！.ID:"+id, CurrentThreadContext.getCurrentUserName());
			return result;
		}
		try {
			entity = this.getRequestParamForRskModify(request);
			entity.setId(Long.valueOf(id));
			boolean success = this.cstCertInfoService.modify(entity);
			if(!success){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_UPDATE_FAIL);
				BaseLogUtils.info(logger, "modifyCstCert", "修改客户证件记录表信息失败.DATA:"+JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
			} else {
				BaseLogUtils.info(logger, "modifyCstCert", "修改客户证件记录表信息成功.DATA:"+JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
			}
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "modifyCstCert", "修改客户证件记录表信息失败.DATA:"+JSONObject.toJSONString(entity)+".ERROR:"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "modifyCstCert", "修改客户证件记录表信息失败,系统错误.DATA:"+JSONObject.toJSONString(entity)+".ERROR:"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}

	/**
	 * 删除数据
	 */
	@RequestMapping(value = "/removeById.do", method = {RequestMethod.GET })
	@ResponseBody
	public DataResultVO removeCstCertById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_DELETE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			BaseLogUtils.info(logger, "removeCstCertById", "删除客户证件记录表信息,主键参数为空！", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		try {
			boolean success = this.cstCertInfoService.removeById(Long.valueOf(id));
			if(!success){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_DELETE_FAIL);
				BaseLogUtils.info(logger, "removeCstCertById", "删除客户证件记录表信息失败,ID:"+id, CurrentThreadContext.getCurrentUserName());
			} else {
				BaseLogUtils.info(logger, "removeCstCertById", "删除客户证件记录表信息成功,ID:"+id, CurrentThreadContext.getCurrentUserName());
			}
		} catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "removeCstCertById", "删除客户证件记录表信息失败,ID:"+id+".ERROR:"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		} catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "removeCstCertById", "删除客户证件记录表信息失败,系统错误.ID:"+id+".ERROR:"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}

		return result;
	}

	/**
	 * 得到一条记录
	 *
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getById.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object getCstCertById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			BaseLogUtils.info(logger, "getCstCertById", "获取客户证件记录表信息,主键参数为空！", CurrentThreadContext.getCurrentUserName());
			return result;
		}

		try {
			CstCertRecVO vo = this.cstCertInfoService.selectById(Long.valueOf(id));
			if(null == vo){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_QUERY_DATA_IS_EMPTY);
				BaseLogUtils.info(logger, "getCstCertById", "获取客户证件记录表信息不存在,ID:"+id, CurrentThreadContext.getCurrentUserName());
			} else {
				result.setData(vo);
				BaseLogUtils.info(logger, "getCstCertById", "获取客户证件记录表信息成功,ID:"+id, CurrentThreadContext.getCurrentUserName());
			}
		}catch(BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "getCstCertById", "获取客户证件记录表信息失败,ID:"+id+".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch(Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "getCstCertById", "获取客户证件记录表信息失败,系统错误.ID:"+id+".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}

	/**
	 * 获取证件请求参数(新增)
	 * @param request 请求
	 * @return
	 */
	private CstCertRecVO getRequestParamForRskAdd(HttpServletRequest request)throws BaseException{
		CstCertRecVO cstCertRecVO = this.getRequestParamForRsk(request);
		cstCertRecVO.setCrtTm(new Date());
		cstCertRecVO.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
		return cstCertRecVO;
	}

	/**
	 * 获取证件请求参数(修改)
	 * @param request 请求
	 * @return
	 */
	private CstCertRecVO getRequestParamForRskModify(HttpServletRequest request)throws BaseException{
		CstCertRecVO cstCertRecVO = this.getRequestParamForRsk(request);
		cstCertRecVO.setMdfTm(new Date());
		cstCertRecVO.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
		return cstCertRecVO;
	}

	/**
	 * 获取证件信息表参数信息
	 * @param request
	 * @return
	 * @throws BaseException
	 */
	private CstCertRecVO getRequestParamForRsk(HttpServletRequest request)throws BaseException{
		String certTypeCd = request.getParameter("certTypeCd");//证件类型
		String certNm = request.getParameter("certNm");//证件姓名
		String certNo = request.getParameter("certNo");//证件号
		String certImg = request.getParameter("certImg");//证件号

		CstCertRecVO cstCertRecVO = new CstCertRecVO();
		cstCertRecVO.setCertTypCd(certTypeCd.trim());
		cstCertRecVO.setCertNm(certNm.trim());
		cstCertRecVO.setCertNo(certNo.trim());
		cstCertRecVO.setCertImg(certImg.trim());
		return cstCertRecVO;
	}
	
}

